window.onload = function () {
    window.vue = new Vue({
        el: '#my-div',
        data: {
            error: '',
            phone: '',
            password: '',
        },
        methods: {
            login: function () {
                vue.error = '正在登陆...';
                doPost({
                    url: 'user/user/login',
                    data: {
                        phone: vue.phone,
                        password: hex_md5(vue.password),
                    },
                    callback: function (res) {
                        if (res.data.code === 1) {
                            localStorage['role'] = res.data.data.role;
                            localStorage['name'] = res.data.data.name;
                            localStorage['phone'] = res.data.data.phone;
                            localStorage['menus'] = res.data.data.menus;
                            localStorage['pwd'] = vue.password;
                            if (res.data.data.role === 'ROOT') {
                                localStorage['click_menu'] = res.data.data.cur_menu.menu;
                                localStorage['click_sub_menu'] = res.data.data.cur_menu.sub_menu;
                            }
                            let c = JSON.stringify({
                                'phone': vue.phone,
                                'pwd': vue.password,
                                'role': res.data.data.role,
                            });
                            c = aes_encrypt(c);
                            c = encodeURIComponent(c);
                            window.location = getLocalStorage('home', 'index.html') + '?c=' + c;
                        } else {
                            vue.error = res.data.msg;
                        }
                    }
                }, false);
            },
            submit: function () {
                if (check()) {
                    if (!window.e){
                        vue.error = '正在拼命加载中...';
                        setTimeout(this.submit, 500);
                    }
                    else {
                        this.login();
                    }
                }
            },
        },
    });
    let c = getQueryVariable('c');
    if (c && c !== 'undefined') {
        c = decodeURIComponent(c);
        c = aes_decrypt(c);
        c = JSON.parse(c);

        vue.phone = c['phone'];
        vue.password = c['pwd'];
        if (c['role'] === 'ROOT') {
            vue.submit();
        }
    }
};

gen_client_key(function (success, key) {
    if (success) {
        let iv = CryptoJS.enc.Latin1.parse('iR9hPO63zXohlw2p');
        window.e = key;
        localStorage['k'] = CryptoJS.AES.encrypt(key, 'c15be48e6ed89c6748c6aebe6d3d5e65', {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        }).toString();
    } else {
        vue.error = key;
    }
});

$(document).keydown(function (event) {
    if (event.keyCode === 13) {
        vue.submit();
    }
});


function check() {
    if (vue.id === '' || vue.password === '') {
        vue.error = '请输入手机号和密码!!!';
        return false;
    }
    return true;
}
